Skip to content

Conversation

@jarent-nvidia
Copy link
Contributor

@jarent-nvidia jarent-nvidia commented Nov 5, 2025

Description of the change:
Added boolean 'autofilter' optional argument to 'DataFrame.to_excel' function to add automatic filters to all columns in the exported excel files. As discussed with @WillAyd starting simple by allowing to add filters only to all or none columns.
New option supported by 'xlsxwriter' and 'openpyxl' engines.

@jarent-nvidia jarent-nvidia marked this pull request as draft November 5, 2025 18:14
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comment but overall looks like a really nice PR. @rhshadrach any thoughts?

@jarent-nvidia jarent-nvidia marked this pull request as ready for review November 5, 2025 21:19
ws = wb.active

assert ws.auto_filter.ref is not None
print(ws.auto_filter.ref)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the print statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print statement removed - thanks for checking!

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm - let's see what others think

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed this is looking very good. Can you add tests for:

  • index=True
  • nonzero startrow / startcol
  • A DataFrame with MultiIndex columns (also called a hierarchical columns) with both merge_cells=True and merge_cells=False.

jarent-nvidia and others added 3 commits November 12, 2025 00:26
feat: Throw an error when autofilter set for 'odf' engine.
feat: Shows warning when autofilter is set together with merge_cells.
chore: Added tests for index=True, startrow/startcol, multindex with merge_cells.
@jarent-nvidia
Copy link
Contributor Author

@rhshadrach I believe the issues I got with the checks is not related to my changes. I'm waiting for #63107 to get all the checks green. Besides that the code changes are ready for your review. I hope I was able to address your feedback. List of changes:

fix: Ensure that index size is considered when setting autofilter range.
feat: Throw an error when autofilter set for 'odf' engine.
feat: Show warning when autofilter is set together with merge_cells.
chore: Added tests for index=True, startrow/startcol, multindex with merge_cell

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous request was for a DataFrame with MultiIndex columns:

columns = MultiIndex(
    levels=[["x", "y"], ["w", "t"]],
    codes=[[0, 0, 1], [0, 1, 0]],
)
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=columns)

but agreed as you've found a MultiIndex index also gives problems. I think we should raise when either columns or index is a MultiIndex, autofilter=True, and merge_cells=True.

Comment on lines 959 to 961
"Excel filters merged cells by showing only the first row."
"'autofiler' and 'merge_cells' should not "
"be used simultaneously.",
Copy link
Member

@rhshadrach rhshadrach Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of blanks, the autofilter here will produce what I consider invalid results. As such, I think we should raise rather than warn.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - exception is raised when autofilter=True, and merge_cells=True

DataFrame().to_excel(writer, autofilter=True, index=False)


@pytest.mark.parametrize("engine", ["xlsxwriter", "openpyxl"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For each test, can you test all engines with something like:

if engine in [...]:
    with pytest.raises(...):
        ...
        return

This makes the test suite more robust when adding a new engine. You might find it easier to move these over to test_writers with TestExcelWriter and the fixtures provided there. But no problem if these stay here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - tests refectored and moved to test_writers module

fix: Handle multindex columns offset
feat: If autofilter is set together with merge_cells, raise an exception
@jarent-nvidia
Copy link
Contributor Author

@rhshadrach feedback addressed. The major code change is related to handling MultiIndex columns - autofiler is added at the bottom row and end range has to be shifted to handle extra blank row added between headers and data.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! One last nit and we're good here.

It should have signature css_declarations string -> excel style.
This is only called for body cells.
autofilter : bool, default False
If True, add automatic filters to all columns
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
If True, add automatic filters to all columns
If True, add automatic filters to all columns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@rhshadrach rhshadrach added this to the 3.0 milestone Nov 18, 2025
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@rhshadrach rhshadrach merged commit 5cffe0c into pandas-dev:main Nov 19, 2025
41 checks passed
@rhshadrach
Copy link
Member

Thanks @jarent-nvidia - fantastic work here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement IO Excel read_excel, to_excel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: adding a filter (and bold) to header when writing to excel

3 participants